<?php

class Clientes_IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	$this->_mapper = new Clientes_Model_ClientesMapper();
    	$this->view->messages = $this->_helper->flashMessenger->getMessages();
    }

    public function indexAction()
    {
        // action body
   
    	$this->view->headTitle()->append('Lista de Clientes');
    	$usuarios = $this->_mapper;
    	$listuser = Zend_Paginator::factory ( $usuarios->listClientes() );
    	$listuser->setCurrentPageNumber ( $this->_getParam( 'pagina', 1 ) )
    	->setItemCountPerPage ( 10 );
    	$this->view->entries = $listuser;
    	
    	 
    }

    public function novoAction()
    {
        
    	$this->view->headTitle()->append('Adicionar Clientes');
    	$form = new Clientes_Form_Clientes();
    	$form->FormCity();
    	$form->renderform("Adicionar");
    	if ($this->getRequest()->isPost()) {
    		$data = $this->getRequest()->getPost();
    		$citybr = new Mylibrary_MySuport_CityBr();
    		$form->getElement('cidade')->addMultiOptions($citybr->getCity($data['estado']));
    		if ($form->isValid($data)) {
    			$clientes = new Clientes_Model_Clientes($form->getValues());
    			$this->_mapper->includeClientes($clientes);
    			$this->_helper->flashMessenger->addMessage('Cliente adicionado com sucesso!');
    			$this->_helper->redirector("index","index","clientes");
    		}else{ 
    			
    			$form->populate($data);
    		}
    		
    	}
    	$this->view->form = $form;

    	
    }


    public function editarAction()
    {
        // action body
    	$this->view->headTitle()->append('Adicionar Clientes');
    	$form = new Clientes_Form_Clientes();
    	
    	
    	$request = $this->getRequest();
    	$id = $request->getParam("id");
    	if(null === $id)$this->_helper->redirector("index","index","clientes");
    	
    	$page = $request->getParam("pagina");
    	if ($this->getRequest()->isPost()) {
    		$formData = $this->_request->getPost();
    		$form->FormCity($formData['estado']);
    		$form->EditForm();
    		$form->renderform("Editar");
    		if ($form->isValid($request->getPost())) {
    			$usuario = new Clientes_Model_Clientes($form->getValues());
    			$this->_mapper->editClientes($usuario, $id);
    			$this->_helper->flashMessenger->addMessage('Cliente editado com sucesso!');
    			$this->_helper->redirector("index","index","clientes",array("pagina"=>$page));
    		}else{
    			$form->FormCity($formData['estado']);
    			$form->renderform("Editar");
    			$form->populate($formData);
    		}
    	}else{
    		$data = $this->_mapper->findpopule($id, new Clientes_Model_Clientes());
    		$form->FormCity($data['estado']);
    		$form->EditForm();
    		$form->renderform("Editar");
    		$form->populate($data);
    	}
    	$this->view->form = $form;
    }

    public function removerAction()
    {
        // action body
    	$this->view->headTitle()->append('Cliente Removido');
    	$request = $this->getRequest();
    	$id = $request->getParam("id");
    	$page = $request->getParam("pagina");
    	$this->_mapper->deletar($id);
    	$this->_helper->flashMessenger->addMessage('Cliente removido com sucesso!');
    	$this->_helper->redirector("index","index","clientes",array("pagina"=>$page));
    }


}











